class Solution {
public:
    int countSubstrings(string s) {
        int n = s.size();

        vector<vector<bool>> dp(n, vector<bool>(n + 1));
        int ret = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = n - 1; j >= i; j--)
            {
                if (s[i] == s[j])
                {
                    if (i == j || i + 1 == j)
                    {
                        dp[i][j] = true;
                    }
                    else
                    {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                    if (dp[i][j]) ++ret;
                }

            }
        }
        return ret;
    }
};